
<template>
    <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start">
       <!-- 轮播图图片列表 -->
       <ul class="carousel-body">
         <li v-for="(item, i) in sliders" :key="item.id" class="carousel-item " :class="{ fade: index === i }">
           <!-- <RouterLink to="/"> -->
             <img :src="item.fileUrl" alt=""  />
           <!-- </RouterLink> -->
         </li>
       </ul>
    
       <!-- 左右两侧的小箭头 -->
       <a href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
       <a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
       <!-- 中间的指示器：5个小圆点 -->
       <div class="carousel-indicator">
         <span :class="{ active: index === i - 1 }" v-for="i in sliders.length" :key="i"></span>
       </div>
     </div>
   
</template>
   
<script setup>
import { ref,  watch, onUnmounted } from 'vue';


const props = defineProps({
    sliders: {
      type: Array,
      default: () => [],
    },
    duration: {
      type: Number,
    //   3000
      default: 3000
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    title: {
        type: String
    }
})

console.log('-----------', props.title,props.sliders );
const index = ref(0);



const timer = ref(null);
const autoplayFn = () => {
    // 防止触发多个定时任务
    clearInterval(timer.value)
    timer.value = setInterval(() => {
    index.value += 1
    if (index.value >= props.sliders.length) {
        // 超过右侧范围,从头开始
        index.value = 0
    }
    }, props.duration * 1000)
}

watch(
    () => props.sliders,
    () => {
    // 轮播图长度大于1并且标志位是放开的
    if (props.sliders.length > 1 && props.autoplay) {
        autoplayFn()
    }
    },
    { immediate: true }
)
 // 进入控制暂停轮播
 const stop = () => {
    if (timer.value) {
      clearInterval(timer.value)
    }
}
// 离开开始轮播
const start = () => {
    console.log(props.sliders)
    if (props.sliders.length > 1 && props.autoplay) {
      autoplayFn()
    }
}

// 组件消耗，清理定时器
onUnmounted(() => {
    clearInterval(timer)
})


</script>
<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      // fade决定那张图显示
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    //    opacity: 0.5;
    }
  }
}
</style>